package ru.sushka.lunch.gui.controllers;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

import ru.sushka.lunch.enitiy.Course;
import ru.sushka.lunch.enitiy.Customer;
import ru.sushka.lunch.enitiy.Order;
import ru.sushka.lunch.gui.AbstractOrderController;
import ru.sushka.lunch.gui.AbstractOrderModel;
import ru.sushka.lunch.gui.views.OrderForm;

public class OrderController implements AbstractOrderController {
    
    private OrderForm orderForm;
    private AbstractOrderModel orderModel;
    private Customer currentCustomer;
    
    private Map<TableItem, Order> orderMap;
    private Map<TableItem, Course> menuMap;
    
    private int totalCost;
    private int currentCustomerCost;
    
    public OrderController(AbstractOrderModel orderModel, Customer customer){
        orderMap = new HashMap<TableItem, Order>();
        menuMap = new HashMap<TableItem, Course>();
        
        this.orderModel = orderModel;
        this.currentCustomer = customer;
        
        orderModel.loadOrderList(customer);
        
        orderForm = new OrderForm(this);
        orderForm.open();
    }
    
    @Override
    public void addOrder(TableItem menuTableSelectedItem) {    
        Course selectedMenu = menuMap.get(menuTableSelectedItem);
        int count = 1;
        if(selectedMenu.isCountable()){
            count = inputCountDialog();
            if(count == 0){
                return;
            }
        }
        Order order = new Order(selectedMenu.getId(),currentCustomer.getId(), count, orderModel.getOrderDate());
        orderModel.addOrder(order);
        loadOrderList();
    }

    @Override
    public void removeOrder(TableItem orderTableSelectedItem) {
        if(orderMap.containsKey(orderTableSelectedItem)){
            orderModel.removeOrder(orderMap.get(orderTableSelectedItem));
            loadOrderList();
        }
    }

    @Override
    public void loadMenuList() {
        Table menuTable = orderForm.getMenuTable();
        menuTable.removeAll();
        menuMap.clear();
        List<Course> menuList = orderModel.getMenuList();
        if(!menuList.isEmpty()){
            for (Course course : menuList) {
                TableItem item = new TableItem(menuTable, 0);
                item.setText(0, course.getCategoryName());
                item.setText(1, course.getName());
                item.setText(2, String.valueOf(course.getPrice()));
                menuMap.put(item, course);
            }
        }
    }

    @Override
    public void loadOrderList() {
        Table orderTable = orderForm.getOrderTable();
        orderTable.removeAll();
        orderMap.clear();
        List<Order> orderList = orderModel.getOrderList(currentCustomer);
        if(!orderList.isEmpty()){
            for (Order order : orderList) {
                TableItem item = new TableItem(orderTable, 0);
                Course course = orderModel.getCourse(order.getMenuItemId());
                // item.setText(0,course.getCategoryName());
                item.setText(0, course.getName());
                item.setText(1, String.valueOf(course.getPrice()));
                item.setText(2, String.valueOf(order.getCount()));
                orderMap.put(item, order);
            }
        }
        totalCost = orderModel.getTotalOrderCost(currentCustomer);
        currentCustomerCost = orderModel.getCustomerOrderCost();
        
    }

    @Override
    public Customer getCustomer() {
        return currentCustomer;
    }

    @Override
    public void saveOrderList() {
        orderModel.saveOrderList(currentCustomer);
    }

    
    @Override
    public boolean isMenuExpired(){
        Calendar date = orderModel.getMenuExpiryDate();
        //Заказ на завтра должен быть сделан сегодня не позднее 15-00
        //TODO: properties!
        date.add(Calendar.DAY_OF_MONTH, -1);
        date.set(Calendar.HOUR_OF_DAY, 15);
        return Calendar.getInstance().before(date);
    }
    
    private int inputCountDialog(){
        int count = 1;
        try{
            String input = orderForm.showInputDialog("Количество", "Введите количество", "1");
            //диалог возвращает null если нажали отмену
            if(input == null){
                return 0;
            }
            count = Integer.valueOf(input);
        //если ввели не число
        } catch(NumberFormatException e) {
            return inputCountDialog();
        }    
        return count;
    }

    public int getTotalCost() {
        return totalCost;
    }

    public void setTotalCost(int totalCost) {
        this.totalCost = totalCost;
    }

    public int getCurrentCustomerCost() {
        return currentCustomerCost;
    }

    public void setCurrentCustomerCost(int currentCustomerCost) {
        this.currentCustomerCost = currentCustomerCost;
    }
}
